package network;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import org.eclipse.swt.graphics.Color;

import com.swtdesigner.SWTResourceManager;

import logic.Player;

import gamemngr.GameMngr;
import gui.RiskGUI;

public class NetworkGame implements Serializable{

	private static final long serialVersionUID = 1L;
	private int gameID;
	private List<NetworkPlayer> players;
	private NetworkPlayer admin;
	private int wantedPlayers;
	private GameMngr game;
	private AtomicInteger numberOfPlayers = new AtomicInteger(0);

	public static final Color[] playerColours = {
		SWTResourceManager.getColor(216, 47, 43),
		SWTResourceManager.getColor(131, 171, 110),
		SWTResourceManager.getColor(117, 137, 174),
		SWTResourceManager.getColor(211, 211, 78),
		SWTResourceManager.getColor(135, 108, 170),
		SWTResourceManager.getColor(172, 146, 111)
	};

	public NetworkGame(NetworkPlayer admin, int wantedPlayers, int gameID){
		this.game = new GameMngr(gameID);
		this.players = new LinkedList<NetworkPlayer>();
		this.admin = admin;
		addPlayer(admin);
		this.gameID = gameID;
		this.setWantedPlayers(wantedPlayers);
	}

	public void addPlayer(NetworkPlayer player) {
		if(isExists(player)){
			player.setName(player.getName() + "1");
		}

		getPlayers().add(player);
		game.addPlayer(new Player(player.getName(), playerColours[numberOfPlayers.getAndIncrement()], game));

		if(getPlayers().size() == getWantedPlayers()){
			Server.openGames.remove(this.getGameID());
			Server.closedGames.put(this.getGameID(), this);	
			//start palying
		}
	}

	private boolean isExists(NetworkPlayer player) {
		for(NetworkPlayer np : this.getPlayers()){
			if(np.getName().equals(player.getName()))
				return true;
		}
		return false;
	}
	
	public void removePlayer(NetworkPlayer player){
		players.remove(player);
	}

	public int numberOfPlayers() {
		return getPlayers().size();
	}

	/**
	 * @return the game
	 */
	public GameMngr getGame() {
		return game;
	}

	/**
	 * @return the players
	 */
	public List<NetworkPlayer> getPlayers() {
		return players;
	}

	/**
	 * @param wantedPlayers the wantedPlayers to set
	 */
	public void setWantedPlayers(int wantedPlayers) {
		this.wantedPlayers = wantedPlayers;
	}

	/**
	 * @return the wantedPlayers
	 */
	public int getWantedPlayers() {
		return wantedPlayers;
	}

	/**
	 * @param gameID the gameID to set
	 */
	public void setGameID(int gameID) {
		this.gameID = gameID;
	}

	/**
	 * @return the gameID
	 */
	public int getGameID() {
		return gameID;
	}

	public void informAll(NetworkMsg msg) {
		for(NetworkPlayer player : getPlayers()){
			player.sendMsg(msg);
		}
	}

	public void sendStartGame() {
		for(int i=0; i<players.size(); i++){
			players.get(i).sendMsg(new GameStarted(this, getGame(),i, players.get(0) + ":"));
		}

	}

	/**
	 * @param game the game to set
	 */
	public void setGame(GameMngr game) {
		this.game = game;
	}

	/**
	 * @return the admin
	 */
	public NetworkPlayer getAdmin() {
		return admin;
	}
}
